package pl.elka.simulator.gui.swt;

import java.util.List;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;

import pl.elka.simulator.core.Simulator;
import pl.elka.simulator.core.configurationCommand.ConfigurationCommandType;
import pl.elka.simulator.core.mobilityManager.MobilityManagerType;
import pl.elka.simulator.mediator.ActionResult;
import pl.elka.simulator.mediator.ActionType;
import pl.elka.simulator.mediator.ActionValue;
import pl.elka.simulator.mediator.SimulationFasade;

public class CommandsComposite extends Composite{

	private final String managerId;
	public CommandsComposite(Composite parent,String mobilityManagerId) {		
		super(parent, SWT.BORDER);
		this.managerId = mobilityManagerId;
		
		GridLayout gl = new GridLayout(1, true);
		setLayout(gl);
		
		if(!mobilityManagerId.equals(Simulator.enviromentConfigurationId)){
			Button runExperimentButton = new Button(this, SWT.PUSH);
			runExperimentButton.setLayoutData(new GridData());
			runExperimentButton.setText("Uruchom sumulacje");
			runExperimentButton.addSelectionListener(new SelectionAdapter() {
				
				@Override
				public void widgetSelected(SelectionEvent e) { 
					SimulationFasade.getInstance().handleAction(ActionType.RunSimulation, new ActionValue(managerId));
					SWTMain.reloadAllComponents();
				}
			});
		}
		
		
		ActionResult result = SimulationFasade.getInstance().handleAction(ActionType.GetAllowedConfigurationCommands, new ActionValue(mobilityManagerId));
		List<ConfigurationCommandType> types = (List<ConfigurationCommandType>)result.getResult();
		
		for(ConfigurationCommandType type:types){
			Button r = new Button(this, SWT.RADIO);
			r.setText(type.getName());
			r.setLayoutData(new GridData());
			r.addSelectionListener(new CommandSelectedListener(type,mobilityManagerId));
		}
		
	}

}
